import threading


def Singleton(cls):
    instance = {}
    lock = threading.Lock()
    def _singleton_wrapper(*args, **kargs):
        if cls in instance:
            return instance[cls]
        with lock:
            if cls not in instance:
                _temp_instance = cls(*args, **kargs)
                instance[cls] = _temp_instance
            return instance[cls]

    return _singleton_wrapper
